package com.example.elmmerchants.Create_Shop;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Qualification_Information_Page extends AppCompatActivity {

    String user_email, shop_status, qualification_status, legal_status, location, licence_status, food_licence_status;

    Button next_step, jump;
    TextView shop_information_status, qualification_information_status, legal_information_status, shop_location, licence_picture_is_upload, food_business_licence_picture_is_upload;
    RelativeLayout upload_place, upload_licence_picture, upload_food_business_licence_picture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qualification_information_page);

        user_email = getIntent().getStringExtra("user_email");
        shop_status = getIntent().getStringExtra("shop_information_status");
        qualification_status = getIntent().getStringExtra("qualification_information_status");
        legal_status = getIntent().getStringExtra("legal_information_status");
        location = getIntent().getStringExtra("location");
        licence_status = getIntent().getStringExtra("licence_status");
        food_licence_status = getIntent().getStringExtra("food_licence_status");

        shop_information_status = findViewById(R.id.shop_information_status);
        qualification_information_status = findViewById(R.id.qualification_information_status);
        legal_information_status = findViewById(R.id.legal_information_status);
        jump = findViewById(R.id.jump);
        upload_place = findViewById(R.id.upload_place);
        shop_location = findViewById(R.id.shop_location);
        upload_licence_picture = findViewById(R.id.upload_licence_picture);
        upload_food_business_licence_picture = findViewById(R.id.upload_food_business_licence_picture);
        licence_picture_is_upload = findViewById(R.id.licence_picture_is_upload);
        food_business_licence_picture_is_upload = findViewById(R.id.food_business_licence_picture_is_upload);
        next_step = findViewById(R.id.next_step);

        if (shop_status.equals("已录入")) {
            shop_information_status.setText("已完成");
        }

        if (qualification_status.equals("已录入")) {
            qualification_information_status.setText("已完成");
        }

        if (legal_status.equals("已录入")) {
            legal_information_status.setText("已完成");
        }

        get_location();

        jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (legal_status.equals("已录入")) {
                    Toast.makeText(Qualification_Information_Page.this, "下页已录入", Toast.LENGTH_SHORT).show();
                }
                else {
                    Intent intent = new Intent(Qualification_Information_Page.this, Legal_Information_Page.class);
                    intent.putExtra("user_email", user_email);
                    intent.putExtra("shop_information_status", shop_status);
                    intent.putExtra("qualification_information_status", qualification_status);
                    intent.putExtra("legal_information_status", legal_status);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    void get_location() {
        shop_location.setText(location);
        if (licence_status.equals("已上传")) {
            licence_picture_is_upload.setText("已上传");
            licence_picture_is_upload.setTextColor(Color.rgb(224, 143, 87));
        }
        else {
            licence_picture_is_upload.setText("去上传");
        }

        if (food_licence_status.equals("已上传")) {
            food_business_licence_picture_is_upload.setText("已上传");
            food_business_licence_picture_is_upload.setTextColor(Color.rgb(224, 143, 87));
        }
        else {
            food_business_licence_picture_is_upload.setText("去上传");
        }
        upload_place.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Qualification_Information_Page.this, Choose_Ctiy_Page.class);
                intent.putExtra("user_email", user_email);
                startActivityForResult(intent, 4);
            }
        });

        if (licence_status.equals("去上传")) {
            upload_licence_picture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Qualification_Information_Page.this, Upload_Licence_Picture_Page.class);
                    intent.putExtra("user_email", user_email);
                    startActivityForResult(intent, 3);
                }
            });
        }

        if (food_licence_status.equals("去上传")) {
            upload_food_business_licence_picture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Qualification_Information_Page.this, Upload_Food_Business_Licence_Picture_Page.class);
                    intent.putExtra("user_email", user_email);
                    startActivityForResult(intent, 3);
                }
            });
        }

        next_step.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!shop_location.getText().toString().equals("暂无") && licence_picture_is_upload.getText().toString().equals("已上传") && food_business_licence_picture_is_upload.getText().toString().equals("已上传")) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL connectionSql = new Connection_SQL();
                            Connection connection = connectionSql.connection();
                            String sql = "update `merchant` set qualification_information = ?, shop_place = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 1);
                            preparedStatement.setString(2, location);
                            preparedStatement.setString(3, user_email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                            Intent intent = new Intent(Qualification_Information_Page.this, Set_Shop_Data.class);
                            setResult(1, intent);
                            finish();
                        });
                    });
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 3: {
                if (data != null) {
                    if (data.getStringExtra("food_business_licence_pic") != null) {
                        food_business_licence_picture_is_upload.setText(data.getStringExtra("food_business_licence_pic"));
                        food_business_licence_picture_is_upload.setTextColor(Color.rgb(224, 143, 87));
                    }
                    if (data.getStringExtra("licence_pic") != null) {
                        licence_picture_is_upload.setText(data.getStringExtra("licence_pic"));
                        licence_picture_is_upload.setTextColor(Color.rgb(224, 143, 87));
                    }
                }
                break;
            }
            case 4: {
                if (data != null) {
                    location = data.getStringExtra("shop_place");
                }
                else {
                    location = "暂无";
                }
                shop_location.setText(location);
                break;
            }
        }
    }
}